package algorithm_optimal_solution.LinkedList;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

//在单链表中删除指定值的节点：用栈，将值不等于num的值收集起来，收集完重新连接。直接调整，找到第一个值不等于num的节点作为头节点，之后的节点判断删除即可。
public class RemoveLinkedListEqualNum {

    static class Node{
        private int value;
        private Node node;

        public Node(int value,Node node){
            this.value = value;
            this.node = node;
        }

        public void setValue(int value){
            this.value = value;
        }

        public int getValue(){
            return value;
        }

        public Node getNode() {
            return node;
        }

        public void setNode(Node node){
            this.node = node;
        }
    }

    public void removeLinkedListEqualNumByStack(Node head,int num){
        Stack<Node> stack = new Stack<>();
        while(head != null){
            if(head.getValue() != num){
                stack.push(head);
            }
            head = head.getNode();
        }
        while(!stack.isEmpty()){
            stack.peek().setNode(head);
            head = stack.pop();
        }
        StringBuilder sb = new StringBuilder();
        while(head != null){
            sb.append(head.getValue()).append(" ");
            head = head.getNode();
        }
        System.out.print(sb);
    }

    public void removeLinkedListEqualNum(Node head,int num){
        while(head != null){
            if(head.getValue() != num){
                break;
            }
            head = head.getNode();
        }
        Node pre = head;
        Node node = head;
        while(node != null){
            if(node.getValue() == num){
                pre.setNode(node.getNode());
            }else{
                pre = node;
            }
            node = node.getNode();
        }
        StringBuilder sb = new StringBuilder();
        while(head != null){
            sb.append(head.getValue()).append(" ");
            head = head.getNode();
        }
        System.out.print(sb);
    }

    public static void main(String[] args)throws IOException {
        BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
        int len = Integer.parseInt(scanner.readLine());
        String[] values = scanner.readLine().split(" ");
        Node node = null;
        for(int i = len - 1;i >= 0;i--){
            node = new Node(Integer.parseInt(values[i]),node);
        }
        int num = Integer.parseInt(scanner.readLine());
//        new RemoveLinkedListEqualNum().removeLinkedListEqualNumByStack(node,num);
        new RemoveLinkedListEqualNum().removeLinkedListEqualNum(node,num);
    }
}
